package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossActivityUnionAbilityRankInfo;
import com.yanqu.road.server.manager.union.CrossUnionActivityAbilityRankMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross3UnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;


@Cmd(code = CrossProtocol.C_UNION_GET_MAX_ABILITY_UNION_RANK_LIST, desc = "请求商会历史最高赚钱排行")
public class Cross2GetUnionAbilityRankListCmd extends CrossCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossUnionProto.CrossUnionGetMaxAbilityUnionRankListReq crossReqMsg = CrossUnionProto.CrossUnionGetMaxAbilityUnionRankListReq.parseFrom(packet.getBytes());
        CrossActivityUnionAbilityRankInfo rankInfo;

        UnionActivityGroup groupData = Cross3UnionActivityGroupMgr.getGroupData(crossReqMsg.getActivityId(), crossReqMsg.getGroupId());
        if (groupData != null) {
            rankInfo = CrossUnionActivityAbilityRankMgr.getRankInfoByGroup(crossReqMsg.getActivityId(), crossReqMsg.getGroupId(), crossReqMsg.getRankNum());
        } else if (crossReqMsg.hasGroupId() && crossReqMsg.getGroupId() > 0) {
            groupData = Cross3UnionActivityGroupMgr.getGroupData(crossReqMsg.getActivityId(), crossReqMsg.getGroupId());
            if (groupData == null) {
                log.info("这里不可能会取不到分组数据，活动{}分组{}", crossReqMsg.getActivityId(), crossReqMsg.getGroupId());
                return;
            }
            rankInfo = CrossUnionActivityAbilityRankMgr.getRankInfoByGroup(crossReqMsg.getActivityId(), crossReqMsg.getGroupId(), crossReqMsg.getRankNum());
        } else {
            rankInfo = CrossUnionActivityAbilityRankMgr.getRankInfo(crossReqMsg.getActivityId(), crossReqMsg.getServerIdsList(), crossReqMsg.getRankNum());
        }

        rankInfo.setActivityEndTime(crossReqMsg.getActivityEndTime());
        MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(Protocol.S_PEAK_UNION_MAX_ABILITY_RANK_UPDATE, rankInfo.getRankMsg()));
    }

    @Override
    public void sendData() {

    }
}
